<?php
namespace app\index\controller;
use app\index\controller\BaseController;
use  Think\DB;

class JiangpinController extends BaseController {
	protected function _initialize() {
		parent::_initialize();
	}
	//兑换大奖
	public function ex_dj(){
		$user_id=session('user_info')['id'];
		$user=db('member')->where("id",$user_id)->field("open_id")->find();	
		if(!$user){
			return json(['code'=>400,'msg'=>'兑换失败']);
		}
		
		$ticket_config = db('system')->field('value')->where('name', 'gold_config')->find();
	    $ticket_config = unserialize($ticket_config['value']);
		if(strtotime($ticket_config['begin_time'])>strtotime(now_time())){
			return json(['code'=>400,'msg'=>'兑奖日期未到']);
		}
		
		$log=db('hb_list')->where('user_id',$user_id)->find();
		if($log){
			return json(['code'=>400,'msg'=>'不能重复兑奖']);
		}
		$list = db("action_item")->where("type",1)->order("sort asc")->select();
		$total=$ticket_config['gold_pool'];
		$pepoles=$ticket_config['gold_people'];
		
		Db::startTrans();		
		$hbcount = db("hb_list")->count();
		if($hbcount>=$pepoles){
			return json(['code'=>400,'msg'=>'现金奖已领取完']);
		}
		
		foreach($list as $k=>$v){
			$w['item_id']=$v['id'];
			$w['user_id']=$user_id;
			$w['nums']=[">=",1];
			$res=db("user_items")->where($w)->setDec("nums",1);
			if(!$res){
				 Db::rollback();
				return json(['code'=>400,'msg'=>'矿石数量不足兑换失败']);
			}
		}

		$balance=$total/$pepoles;
		$title='矿博会现金大奖';
		$data['user_id']=$user_id;
		$data['balance']=$balance;
		$data['message']=$title;	
		$data['add_time']=now_time();
		$res=db("hb_list")->insert($data);
		if(!$res){
			Db::rollback();
			return json(['code'=>400,'msg'=>'兑换失败']);
		}
		Db::commit();
		$this->send_hb($user['open_id'], $balance, $title);
		return json(['code'=>200,'msg'=>'兑换成功']);
	}
		//红包发放记录
	public function send_hb($openId,$price,$title){
		import("payment.wxpay.WxPay");
		//①、获取当前访问页面的用户openid（如果给指定用户发送红包，则填写指定用户的openid)
		$wxPay = new \WxpayService();

		if(!$openId) exit('获取openid失败');
		//②、发送红包
		$outTradeNo = order_create_no();     //你自己的商品订单号
		$payAmount = $price;          //红包金额，单位:元
		$sendName = $title;    //红包发送者名称
		$wishing = '矿博会现金大奖！';      //红包祝福语
		$act_name='矿博会现金大奖';           //活动名称
		$result = $wxPay->createJsBizPackage($openId,$payAmount,$outTradeNo,$sendName,$wishing,$act_name);
		file_put_contents("hb.txt", $result);
		return true;
		//echo 'success';
	}
	//兑换实物奖品
	public function ex_jp(){
		$user_info=session('user_info');
		if(!$user_info){
			return json(['code'=>400,'msg'=>'核销失败']);
		}
		
		$where['prize_id'] = input("id");
		$where['status'] = 2;
		$count = db("prizelogs")->where($where)->count();

		$w['user_id']=$user_info['id'];
		$w['item_id']=input("id");
		$cz=db("user_items")->where($w)->setDec("nums",1);
		if(!$cz){
			return json(['code'=>400,'msg'=>'核销失败']);
		}
		$data['user_id']=$user_info['id'];
		$data['prize_id']=input("id");
		$data['add_time']=now_time();
		$data['mobile']=$user_info['mobile'];
		$data['status']=2;
		$res=db("prizelogs")->insert($data);
		if(!$res){
			return json(['code'=>400,'msg'=>'兑换失败']);
		}else{
			return json(['code'=>200,'msg'=>'兑换成功']);
		}
	}
	public function ex_jp1(){
		$user_info=session('user_info');
		if(!$user_info){
			return json(['code'=>400,'msg'=>'领取失败']);
		}
		$jp=db('action_prize')->where("id",input("id"))->find();
		$num=$jp['num'];
		$logs=db('prizelogs')->where("prize_id",input("id"))->count();
		if($logs&&$logs>=$num){
			return json(['code'=>400,'msg'=>'兑换失败奖品剩余数量为0']);
		}else{
			Db::startTrans();
			$items=explode(",", $jp['demand']);
			$w['user_id']=$user_info['id'];
			$w['nums']=[">=",1];
			foreach($items as $k=>$v){
				$w['item_id']=$v['id'];
				$res=db("user_items")->where($w)->setDec("nums",1);
				if(!$res){
					 Db::rollback();
					return json(['code'=>400,'msg'=>'矿石数量不足兑换失败']);
				}
			}
			$data['user_id']=$user_info['id'];
			$data['prize_id']=input("id");
			$data['add_time']=now_time();
			$data['mobile']=$user_info['mobile'];
			$res=db("prizelogs")->insert($data);
			if(!$res){
				Db::rollback();
					return json(['code'=>400,'msg'=>'兑换失败']);
			}else{
				Db::commit();
				return json(['code'=>200,'msg'=>'兑换成功']);
			}
		}
		
	}
	public function lingqu(){
		$agent_id=input('agent_id');
		$agent=db("agent")->where("id",$agent_id)->find();
		$this->assign('agent',$agent);
		return $this->fetch();
	}
	public function lingqu_sb1(){
		return $this->fetch();
	}
	//随机抽奖矿石
	public function choujiang(){
		$user_id=session("user_info")['id'];
		if(!$user_id){
			echo '用户未注册领取失败';exit;
		}
		$agent_id=input('agent_id');
		
		//要有电子票，才享有扫码领矿石资格
		$where['user_id'] = $user_id;
		$where['type'] = 1;  //电子票
		$where['status'] = 2;  //完成支付
		$count = db("ticketlogs")->where($where)->count();
		if($count==0){
			return $this->fetch("lingqu_sb1");
		}
		$aw["num"]=[">",0];
		$aw['state']=1;
		//$jps=db("action_prize")->where($aw)->select();
		$items=db("action_item")->where($aw)->field('id,thumb,name,probability')->select();
		$list=array();
		$list1=array();
		if(!$items){
			return $this->fetch("lingqu_sb");
		}
		foreach($items as $k=>$v){
			$list1[$v['id']]=$v;
			$list[$v['id']]=$v['probability'];
		}
		$rid = get_rand($list); //根据概率获取奖项id
		$item=$list1[$rid];
		$w['user_id']=$user_id;
		$w['agent_id']=$agent_id;
		$logs=db("action_log")->where($w)->wheretime("add_time","today")->select();
		if($logs&&count($logs)>=1){
			return $this->fetch("lingqu_sb");
		}else{
			$wh['user_id']=$user_id;
			$wh['item_id']=$rid;
			$lq=db('user_items')->where($wh)->find();
			$data['user_id']=$user_id;
			$data['item_id']=$rid;
			if($lq){
				db("user_items")->where($wh)->setInc("nums",1);
			}else{
				db("user_items")->insert($data);
			}
			$data['add_time']=now_time();
			$data['agent_id']=$agent_id;
			db("action_item")->where('id',$rid)->setDec("num",1);
			$res=db('action_log')->insert($data);
		}
		
		$this->assign('item',$item);
		return $this->fetch("lingqu_cg");
	}
	public function lingqu_cg(){
		return $this->fetch();
	}
	public function lingqu_sb(){
		return $this->fetch();
	}
}